package m2dl.ter.mirrorgame;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MenuActivity extends Activity {

	protected MediaPlayer mediaPlayer = null;

	/** The file persistent path. */
	protected final String PREFS_SCORE = "file";

	/** Attributes classes. */
	protected Button playGameButton, exitGameButton;

	/** The textView for the score. */
	protected TextView newScoreTextView = null, bestScoreTextView = null;

	/** The manager of the score. */
	SharedPreferences pref = null;

	/** Gets and shows the persistentScore. */
	@SuppressLint("WorldReadableFiles")
	private void showThePersistentScore() {
		// Gets and Shows the persistent score
		this.pref = this.getSharedPreferences(PREFS_SCORE, MODE_WORLD_READABLE);
		String bestScore = " " + this.pref.getInt("score", 0);
		String newScore = " " + this.pref.getInt("newScore", 0);
		this.bestScoreTextView.setText(bestScore);
		this.newScoreTextView.setText(newScore);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_menu);

		// Manages the playGameButton
		this.playGameButton = (Button) findViewById(R.id.id_menu_button_play);
		this.playGameButton.setVisibility(View.VISIBLE);

		// Manages the playGameButton
		this.exitGameButton = (Button) findViewById(R.id.id_menu_button_exit);
		this.exitGameButton.setVisibility(View.VISIBLE);

		// Gets and Shows the persistent score
		this.bestScoreTextView = (TextView) findViewById(R.id.id_menu_bestScore);
		this.newScoreTextView = (TextView) findViewById(R.id.id_menu_currentScore);
		
		this.showThePersistentScore();
		
		this.playGameButton.setOnClickListener( new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(MenuActivity.this, Choregraphy.class);
				startActivity(intent);
				System.exit(RESULT_OK);
			}			
		});
		this.exitGameButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public void onStart() {
		super.onStart();
		// Gets the persistent score
		this.showThePersistentScore();
	}

	protected void onResume() {
		super.onResume();
		Intent intent = getIntent();
		
		if (intent != null) {
			this.mediaPlayer = MediaPlayer.create(getBaseContext(),R.raw.bravo);
			this.mediaPlayer.start();
		}
		
	}

	@Override
	public void onStop() {
		super.onStop();
		// this.finish();
	}

	// Manages the menu
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_quitter:
			finish();
			return true;
		case R.id.action_settings:
			Toast.makeText(this, "Application with no settings on version 1",
					Toast.LENGTH_LONG).show();
			return true;
		}
		return false;
	}
}
